home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / microemacs / termio.c.save < prev    next >
Text File  |  1992-05-06  |  4KB  |  155 lines

  1. /*
  2.  * The functions in this file negotiate with the operating system for
  3.  * characters, and write characters in a barely buffered fashion on the display.
  4.  * All operating systems.
  5.  */
  6. #include <exec/types.h>
  7. #include <exec/exec.h>
  8. #include <intuition/intuition.h>
  9. #include <devices/console.h>
  10. #include <stdio.h>
  11. #include "ed.h"
  12. #include "keymap.h"
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15. #define INTUITION_REV 29
  16.  
  17. static struct NewWindow NewWindow = {
  18.    0, 0,
  19.    640, 200,
  20.    -1, -1,
  21.    0,
  22.    SMART_REFRESH | ACTIVATE | WINDOWDRAG | WINDOWDEPTH | WINDOWSIZING,
  23.    NULL,
  24.    NULL,
  25.    "MicroEMACS",
  26.    NULL,
  27.    NULL,
  28.    100, 35,
  29.    640, 200,
  30.    WBENCHSCREEN
  31. };
  32.  
  33. static struct Window *Window;
  34. static struct IOStdReq consoleIO;
  35. static struct MsgPort consoleMsgPort;
  36.  
  37. #define NOBUF 1024
  38. static char obuf[NOBUF];
  39. static int nobuf;
  40.  
  41. /*
  42.  * This function is called once to set up the terminal device streams.
  43.  * On VMS, it translates SYS$INPUT until it finds the terminal, then assigns
  44.  * a channel to it and sets it raw. On CPM it is a no-op.
  45.  */
  46. ttopen()
  47. {
  48.    IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",
  49.                             INTUITION_REV);
  50.    if (IntuitionBase == NULL)
  51.    {  printf("can't open Intuition\n"); exit(1); }
  52.    Window = (struct Window *)OpenWindow(&NewWindow);
  53.    if (Window == NULL)
  54.    {  printf("can't open window\n"); exit(1); }
  55.    consoleIO.io_Data = (APTR) Window;
  56.    consoleIO.io_Length = sizeof(*Window);
  57.    if (OpenDevice("console.device", 0, &consoleIO, 0) != 0)
  58.    {  printf("can't open console\n"); exit(1); }
  59.    consoleMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  60.    consoleMsgPort.mp_Flags = 0;
  61.    consoleMsgPort.mp_SigBit = AllocSignal(-1);
  62.    consoleMsgPort.mp_SigTask = (struct Task *)FindTask(NULL);
  63.    consoleIO.io_Message.mn_ReplyPort = &consoleMsgPort;
  64.  
  65.    consoleIO.io_Command = CD_SETKEYMAP;
  66.    consoleIO.io_Data = (APTR) &KeyMap;
  67.    consoleIO.io_Length = sizeof(KeyMap);
  68.    DoIO(&consoleIO);
  69.  
  70.    nobuf = 0;
  71. }
  72.  
  73. /*
  74.  * This function gets called just before we go back home to the command
  75.  * interpreter. On VMS it puts the terminal back in a reasonable state.
  76.  * Another no-operation on CPM.
  77.  */
  78. ttclose()
  79. {
  80.    ttflush();
  81.    CloseDevice(&consoleIO);
  82.    CloseWindow(Window);
  83.    CloseLibrary(IntuitionBase);
  84. }
  85.  
  86. /*
  87.  * Write a character to the display. On VMS, terminal output is buffered, and
  88.  * we just put the characters in the big array, after checking for overflow.
  89.  * On CPM terminal I/O unbuffered, so we just write the byte out. Ditto on
  90.  * MS-DOS (use the very very raw console output routine).
  91.  */
  92. ttputc(c)
  93. {
  94.         if (nobuf >= NOBUF)
  95.                 ttflush();
  96.         obuf[nobuf++] = c;
  97. }
  98.  
  99. /*
  100.  * Flush terminal buffer. Does real work where the terminal output is buffered
  101.  * up. A no-operation on systems where byte at a time terminal I/O is done.
  102.  */
  103. ttflush()
  104. {
  105.    if (nobuf != 0)
  106.    {
  107.       long old[15], new[15];
  108.       int i;
  109.       
  110.       consoleIO.io_Command = CMD_WRITE;
  111.       consoleIO.io_Data = (APTR) obuf;
  112.       consoleIO.io_Length = nobuf;
  113.       setjmp(old);
  114.       DoIO(&consoleIO);
  115.       setjmp(new);
  116.       for (i = 1; i < 15; ++i)
  117.       {
  118.          if (old[i] != new[i])
  119.         if (i < 8)
  120.            printf("ttflush: [D%d] 0x%08x->0x%08x\n", i, old[i], new[i]);
  121.         else
  122.            printf("ttflush: [A%d] 0x%08x->0x%08x\n", i - 7, old[i], new[i]);
  123.       }
  124.       nobuf = 0;
  125.    }
  126. }
  127.  
  128. /*
  129.  * Read a character from the terminal, performing no editing and doing no echo
  130.  * at all. More complex in VMS that almost anyplace else, which figures. Very
  131.  * simple on CPM, because the system can do exactly what you want.
  132.  */
  133. ttgetc()
  134. {
  135.    char ch;
  136.    long old[15], new[15];
  137.    int i;
  138.  
  139.    consoleIO.io_Command = CMD_READ;
  140.    consoleIO.io_Data = (APTR) &ch;
  141.    consoleIO.io_Length = 1;
  142.       setjmp(old);
  143.    DoIO(&consoleIO);
  144.       setjmp(new);
  145.       for (i = 1; i < 15; ++i)
  146.       {
  147.          if (old[i] != new[i])
  148.         if (i < 8)
  149.            printf("ttgetc: [D%d] 0x%08x->0x%08x\n", i, old[i], new[i]);
  150.         else
  151.            printf("ttgetc: [A%d] 0x%08x->0x%08x\n", i - 7, old[i], new[i]);
  152.       }
  153.    return((int)ch);
  154. }
  155.